package gold.digger;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC1021 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public String removeOuterParentheses(String s) {
            Stack<Character> st = new Stack<>();
            StringBuilder res = new StringBuilder();
            int lastPos = 0;

            for (int i = 0; i < s.length(); i++) {
                if (!st.isEmpty() && st.peek() == '(' && s.charAt(i) == ')') {
                    st.pop();
                    if (st.isEmpty()) {
                        //当前以及情况，需要拼接res
                        res.append(s.substring(lastPos + 1, i));
                        lastPos = i + 1;
                    }
                } else {
                    st.push(s.charAt(i));
                }
            }

            return res.toString();
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.removeOuterParentheses("()()"));
    }

    public static void main(String[] args) throws Exception {
        LC1021 an = new LC1021();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
